#!/bin/bash

. ./cmd.sh ## You'll want to change cmd.sh to something that will work on your system.
           ## This relates to the queue.

. ./path.sh
srcDir=exp/dnn6_fbank_1124_delta_dbn_dnn
dataDir=exp_pdnn/dnn6_fbank_new/data
dir=speaker-sensitivity/exp_pdnn/dnn6_fbank_1124_delta_dbn_dnn
mkdir -p $dir
dir=`perl -e '($dir,$pwd)= @ARGV; if($dir!~m:^/:) { $dir = "$pwd/$dir"; } print $dir; ' $dir ${PWD}`
stage=-3


if [ $stage -le -5 ]; then
###################### Create Alignments to PDF Information ########################
alignDir=speaker-sensitivity/alignments
mkdir =p $alignDir

echo "get pdf and phone information "
 for x in MEGJ0 MTDB0 FSAH0 MSTF0 MABC0 FDAW0 MJAE0 MRJB1 MJRK0 MDHS0 MPSW0 MTBC0 MSVS0 MLEL0 FGRW0 FLJA0 FPAB1 MPGR0 FPAF0 FLAC0 MSDB0 FEXM0 MJKR0 MMWS0 MRCG0 MKAH0 MREE0 MTRR0 MZMB0 FDJH0 MMEA0 FSKL0 FKAA0 MWRE0 MILB0 FBLV0 MPRK0 MDJM0 MRSP0 MDLC1; do
  echo " processing speaker $x "
  aliDir=exp_pdnn/${x}_ali
  gunzip -c $aliDir/ali.1.gz | ali-to-pdf $aliDir/final.mdl  ark:-  ark,t:$alignDir/pdf.$x.ark
  gunzip -c $aliDir/ali.1.gz | ali-to-phones --per-frame=true $aliDir/final.mdl  ark:-  ark,t:$alignDir/phones.$x.ark
 done
fi



if [ $stage -le -4 ]; then
###################### Create nnets to get hidden activations ########################
echo "Creating intermediate nnets"
#nnet-concat exp/tri4b_pretrain-dbn_dnn/final.feature_transform $dnn $dir/final_full.net
nnet-copy --remove-last-layers=12 $srcDir/dnn.nnet $dir/final_1.nnet
nnet-copy --remove-last-layers=10 $srcDir/dnn.nnet $dir/final_2.nnet
nnet-copy --remove-last-layers=8 $srcDir/dnn.nnet $dir/final_3.nnet
nnet-copy --remove-last-layers=6 $srcDir/dnn.nnet $dir/final_4.nnet
nnet-copy --remove-last-layers=4 $srcDir/dnn.nnet $dir/final_5.nnet
nnet-copy --remove-last-layers=2 $srcDir/dnn.nnet $dir/final_6.nnet
echo "nnet creation successful"
fi



if [ $stage -le -3 ]; then
################### Generate Training Hidden Activations ####################################
#featureDir=speaker-sensitivity/feats/fbank_delta
activationDir=$dir/activations/

for layer in $(seq 1 6);do
 h=$activationDir/h$layer
 mkdir -p $h
 echo "The hidden activations will be stored in $h directory"
 for x in MEGJ0 MTDB0 FSAH0 MSTF0 MABC0 FDAW0 MJAE0 MRJB1 MJRK0 MDHS0 MPSW0 MTBC0 MSVS0 MLEL0 FGRW0 FLJA0 FPAB1 MPGR0 FPAF0 FLAC0 MSDB0 FEXM0 MJKR0 MMWS0 MRCG0 MKAH0 MREE0 MTRR0 MZMB0 FDJH0 MMEA0 FSKL0 FKAA0 MWRE0 MILB0 FBLV0 MPRK0 MDJM0 MRSP0 MDLC1; do


grep ^$x $dataDir/train/feats.scp | copy-feats scp:- ark:- | add-deltas --delta-order=2 ark:- ark:- | splice-feats --left-context=5 --right-context=5 ark:- ark:- | nnet-forward --feature-transform=$srcDir/feature_transform $dir/final_$layer.nnet ark:- ark,t:$h/activations_$x.ark

#grep ^$x $srcDir/data/train/feats.scp | copy-feats scp:- ark:-  | splice-feats --left-context=5 --right-context=5 ark:- ark:- | nnet-forward --feature-transform=$srcDir/feature_transform $dir/final_$layer.nnet ark:- ark,t:$h/activations_$x.ark


#   apply-cmvn --norm-vars=true --utt2spk=ark:exp_pdnn/100_Str_25_dnn_fbank_128/data/train/${x}/utt2spk scp:exp_pdnn/100_Str_25_dnn_fbank_128/data/train/$x/cmvn.scp scp:exp_pdnn/100_Str_25_dnn_fbank_128/data/train/$x/feats.scp ark:- | splice-feats --left-context=5 --right-context=5 ark:- ark:- | nnet-forward $dir/final_$layer.nnet ark:- ark,t:$h/activations_$x.ar
 done
 echo "Dumping activations for training dataset and $layer is completed"
done


#echo "generating activations for testing speakers"

#for layer in $(seq 1 5);do
# h=$activationDir/h$layer
# mkdir -p $h
# echo "The hidden activations will be stored in $h directory"
# for x in FDHC0 FELC0 FJLM0 FMGD0 FMLD0 FNLP0 FPAS0 FPKT0 MBPM0 MCMJ0 MDAB0 MGRT0 MJDH0 MJLN0 MJMP0 MKLT0; do
#   apply-cmvn --norm-vars=true --utt2spk=ark:exp_pdnn/100_Str_25_dnn_fbank_128/data/test/${x}/utt2spk scp:exp_pdnn/100_Str_25_dnn_fbank_128/data/test/$x/cmvn.scp scp:exp_pdnn/100_Str_25_dnn_fbank_128/data/test/$x/feats.scp ark:- | splice-feats --left-context=5 --right-context=5 ark:- ark:- | nnet-forward $dir/final_$layer.nnet ark:- ark,t:$h/activations_$x.ark
# done
# echo "Dumping activations for testing dataset and $layer is completed"
#done




fi






echo "Dumping Process is completed"
